<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>回调</title>

  <script src="js/pulgins/vue/dist/vue.js"></script>
  <script src="js/pulgins/axios/dist/axios.js"></script>
  <script src="js/pulgins/common.js"></script>

</head>
<body>
<div id="myDiv">

</div>
<script type="text/javascript">
  new Vue({
    el:"#myDiv",
    mounted(){
      //解析参数对象
      let url = location.href;
      let paramObj = this.parseUrlParams2Obj(url);
      console.log(paramObj)
      //获取发送请求参数
      let binderUrl = "http://bugtracker.itsource.cn/binder.html"
      let params = {"code":paramObj.code,"binderUrl":binderUrl};

      //发起微信登录请求
      this.$http.post("/login/wechat",params)
              .then(result=>{
                result = result.data;
                console.log(result)
                if(result.success){ //已经关联了
                  //做登录
                  //提示
                  alert("登录成功！")
                  //把token和loginInfo存放到localStorage
                  let {token,userinfo} = result.data;
                  localStorage.setItem("token",token);
                  //把对象转换为json字符串存放
                  localStorage.setItem("userinfo",JSON.stringify(userinfo));
                  //跳转主页
                  location.href = "/index.html";
                }
              })
              .catch(result=>{
                alert("系统错误");
                console.log(result);
              })
    },
    methods:{
      /**
       * 解析url获取参数对象
       * @param url
       * @returns {{}}
       */
      parseUrlParams2Obj(url){// http://bugtracker.itsource.cn/callback.html?code=061IduGa1839qC02jlIa1NmHxX1IduGX&state=1
        //获取请求参数
        let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
        let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
        let paramObj = {};//对象 json
        for(let i=0;i<paramStrs.length;i++){
          let paramName = paramStrs[i].split("=")[0];//code
          let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
          paramObj[paramName] = paramValue;// 动态给对象设置属性和值
        }
        return paramObj;
      }
    }

  });
</script>
</body>
</html>